
public class ExOnLock {
	
	public static void main(String[] args) {
		ExOnLocking.method1();
		ExOnLocking ex = new ExOnLocking();
		ExOnLocking ex1 = new ExOnLocking();
		ex.method2();
		ex.method2();
		
	}

}
class ExOnLocking {
	
	
	synchronized static void method1(){
		System.out.println("Method1");
	}
	void method2(){
		Object lock = new Object();
		synchronized (lock) {
			System.out.println("Method2");
		}
	}
}
class Bar implements Runnable {
	 
    @Override
    public void run() {
        objectLock();
        
    }
     
    public void objectLock() {
        System.out.println(Thread.currentThread().getName());
        synchronized(this) {
            System.out.println("synchronized block " + Thread.currentThread().getName());
            System.out.println("synchronized block " + Thread.currentThread().getName() + " end");
        }
     
    }
 
    public static void main(String[] args) {
        Bar b1 = new Bar();
        Thread t1 = new Thread(b1);
        Thread t2 = new Thread(b1);
         
        Bar b2 = new Bar();
        Thread t3 = new Thread(b1);
         
        t1.setName("t1");
        t2.setName("t2");
        t3.setName("t3");
         
        t1.start();
        t2.start();
        t3.start();
    }
}



class Bar1 implements Runnable  {
	 
    @Override
    public void run() {
        objectLock();
    }
     
    public void objectLock() {
        System.out.println(Thread.currentThread().getName());
        synchronized(Bar.class) {
            System.out.println("synchronized block " + Thread.currentThread().getName());
            System.out.println("synchronized block " + Thread.currentThread().getName() + " end");
        }
    }
 
    public static void main(String[] args) {
        Bar b1 = new Bar();
        Thread t1 = new Thread(b1);
        Thread t2 = new Thread(b1);
         
        Bar b2 = new Bar();
        Thread t3 = new Thread(b2);
         
        t1.setName("t1");
        t2.setName("t2");
        t3.setName("t3");
         
        t1.start();
        t2.start();
        t3.start();
    }
}